// 引入pdf.js库
// import pdfjsLib from 'pdfjs-dist/legacy/build/pdf.min.js';

var pdfjsLib = require("pdfjs-dist/build/pdf.js");
import workerSrc from "pdfjs-dist/build/pdf.worker.entry"

pdfjsLib.GlobalWorkerOptions.workerSrc = workerSrc;

// import pdfjsLib from "pdfjs-dist";
// import workerSrc from 'pdfjs-dist/build/pdf.worker.entry'
// pdfJS.workerSrc = workerSrc;
// import pdfjsLib from 'pdfjs-dist';
// const PDFJS = require('pdfjs-dist');
// PDFJS.GlobalWorkerOptions.workerSrc = require('pdfjs-dist/build/pdf.worker.entry');

export default{
    install (Vue, options){
        Vue.prototype.pdftoimg = async function(pdfUrl,dom,num){
            console.log(pdfUrl,'8648458144');
            const pdf = await pdfjsLib.getDocument('https://taijieyun.oss-cn-beijing.aliyuncs.com/material/20230427/%E7%85%A4%E6%B0%94%E4%BD%9C%E4%B8%9A%E5%AE%89%E5%85%A8%E6%8A%80%E6%9C%AF%E5%AE%9E%E9%99%85%E6%93%8D%E4%BD%9C3%20%281%29.pdf').promise;
            console.log(pdf,'1234566');
            // 循环遍历每一页pdf，将其转成图片
            for (let i = 1; i <= pdf._pdfInfo.numPages; i++) {
                const canvas = document.createElement('canvas');
                const context = canvas.getContext('2d');
                // 获取pdf页
                const page = await pdf.getPage(i);

                // 获取页的尺寸
                const viewport = page.getViewport({ scale: 1 });
                console.log(viewport,'6666');
                // 设置canvas的尺寸
                canvas.width = viewport.width;
                canvas.height = viewport.height; 

                // 将pdf页渲染到canvas上
                await page.render({ canvasContext: context, viewport: viewport }).promise;

                // 将canvas转成图片，并添加到页面上
                // const img = document.createElement('img');
                // img.src = canvas.toDataURL('image/png');
                // img.style=`width:${595 * 2}px;height:${840*2}px;`
                // img.width = canvas.width
                // img.height = canvas.height
                // width: calc(595.28px * 2);
                //   height: calc(841.89px * 2);
                dom.appendChild(canvas);
                
            }
        }
    }
}